package views;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import models.Oficina;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import controllers.Oficinas;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class BajaCasilla extends javax.swing.JFrame {
	private JTextField txtNombreCasilla;
	private JLabel lblNombreCas;
	private JButton btnCancelar;
	private JButton btnAceptar;
	private JLabel lblArroba;
	private JComboBox comboOficina;
	private DefaultComboBoxModel comboOficinaModel= new DefaultComboBoxModel() ;
	private ActionsListener listener= new ActionsListener();
	private JFrame window= this;
	private JFrame parent;
	private Oficinas controller = Oficinas.getInstancia();
	private List<Oficina> oficinas;
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	
	public BajaCasilla(JFrame parent) {
		super();
		this.parent= parent;
		llenarCombo();
		initGUI();
		
	}
	
	private void llenarCombo() {
		oficinas= controller.getOficinas();
		System.out.println("llenando combo...");
		for(Oficina oficina : oficinas){
			System.out.println(oficina.getNombre());
			comboOficinaModel.addElement(oficina.getNombre());
		}
		System.out.println("Listo!");
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			{
				txtNombreCasilla = new JTextField();
				getContentPane().add(txtNombreCasilla, new AnchorConstraint(289, 467, 408, 169, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				txtNombreCasilla.setPreferredSize(new java.awt.Dimension(205, 23));
			}
			{
				lblNombreCas = new JLabel();
				getContentPane().add(lblNombreCas, new AnchorConstraint(309, 159, 382, 45, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				lblNombreCas.setText("Casilla:");
				lblNombreCas.setPreferredSize(new java.awt.Dimension(78, 14));
			}
			{
				comboOficina = new JComboBox();
				getContentPane().add(comboOficina, new AnchorConstraint(289, 869, 408, 564, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				comboOficina.setModel(comboOficinaModel);
				comboOficina.setPreferredSize(new java.awt.Dimension(210, 23));
			}
			{
				lblArroba = new JLabel();
				getContentPane().add(lblArroba, new AnchorConstraint(304, 537, 388, 494, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				lblArroba.setText("@");
				lblArroba.setPreferredSize(new java.awt.Dimension(30, 16));
			}
			{
				btnAceptar = new JButton();
				getContentPane().add(btnAceptar, new AnchorConstraint(747, 716, 903, 536, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				btnAceptar.setText("Aceptar");
				btnAceptar.setPreferredSize(new java.awt.Dimension(124, 30));
				btnAceptar.addActionListener(listener);
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar, new AnchorConstraint(752, 958, 903, 801, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				btnCancelar.setText("Cancelar");
				btnCancelar.setPreferredSize(new java.awt.Dimension(108, 29));
				btnCancelar.addActionListener(listener);
			}
			pack();
			this.setSize(703, 230);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private class ActionsListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent event) {
			// TODO Auto-generated method stub
			Object source = event.getSource();
				
			if(source == btnAceptar)
			{
				String nombreOficina= (String)comboOficina.getSelectedItem();
				if(controller.bajaCasilla(txtNombreCasilla.getText(), nombreOficina)){
					JOptionPane.showMessageDialog(null, "Se ha eliminado correctamente la casill: "+ txtNombreCasilla.getText()+"@" + nombreOficina + ".com");
					window.dispose();
				}
			}else if(source== btnCancelar){
				window.setVisible(false);
			}
		}
		
	}
}
